﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 绩效管理系统;
using 绩效管理系统.Models;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;

namespace 绩效管理系统
{
    

    public partial class FrmSetUser : Form
    {
        private DelBindDgv _delBindDgv;
        public FrmSetUser(DelBindDgv delBindDgv)
        {
            InitializeComponent();
            _delBindDgv = delBindDgv;
        }
        public Users _user;
        public FrmSetUser(DelBindDgv delBindDgv,int userId):this(delBindDgv)
        {
            _user = Users.ListAll().Find(m => m.Id == userId);
            
        }



        private void cbxBase_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void FrmSetUser_Load_1(object sender, EventArgs e)
        {
            List<AppraisalBases> appraisalBases = new List<AppraisalBases>();
            appraisalBases = AppraisalBases.ListAll();
            //appraisalBases.AddRange(appraisalBases);
            cbxBase.DataSource = appraisalBases;
            cbxBase.DisplayMember = "BaseType";
            cbxBase.ValueMember = "Id";
            //有cbxBase不显示的bug
            if (_user != null)
            {
                cbxusername.Text = _user.UserName;
                cbxBase.SelectedValue = _user.BaseTypeId;

                cbxSex.Text = _user.Sex;
                cbxIsdel.Checked = _user.IsDel;
            }
            


        }

        private void userSave_Click(object sender, EventArgs e)
        {
            string userName = cbxusername.Text;
            string baseTypeId = cbxBase.SelectedValue.ToString();
            string sex = cbxSex.Text;
            bool isDel = cbxIsdel.Checked;
           
            if (_user ==null)
            {
                Users users = new Users
                {
                    UserName = userName,
                    BaseTypeId = baseTypeId,
                    PassWord = "111",
                    IsDel = isDel,
                    Sex = sex
                };
                Users.Insert(users);
                MessageBox.Show("用户添加成功");
            }
            else
            {
                _user.UserName = userName;
                _user.BaseTypeId = baseTypeId;
                _user.IsDel = isDel;
                _user.Sex = sex;
                Users.Update(_user);
                MessageBox.Show("用户修改成功");
            }
            _delBindDgv();
            //this.Close();
        }
    }
}
